/***************************************************************************************
 The copyright in this software is being made available under the BSD License, included
 below. This software may be subject to other third party rights,including patent rights,
 and no such rights are granted under this license.

 Regents of the Tsinghua University, Peking University, Zhejiang University, HKUST
 Tsinghua University, Peking University, Zhejiang University, HKUST 2011

 Copyright (c) 2011, Regents of the Tsinghua University, Peking University, Zhejiang
 University, HKUST. All Rights Reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:
   * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.
   * Neither the names of the copyright holders nor the names of its
     contributors may be used to endorse or promote products derived from this
     software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 OF SUCH DAMAGE.
***************************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "contributors.h"
#include "global.h"
#include "qcoder.h"

prob_state_t eq_prob_fsm[] =
{
    /* 0 */ { 0x5555, 0, 0, 0 }
};
prob_state_t standard_prob_fsm[] =
{
    /* state lpsInterval nextStateLps nextStateMps doSwitchMps */
    /* 0 */ { 0x5a1d, 1, 1, 1 },
    /* 1 */ { 0x2586, 14, 2, 0 },
    /* 2 */ { 0x1114, 16, 3, 0 },
    /* 3 */ { 0x080b, 18, 4, 0 },
    /* 4 */ { 0x03d8, 20, 5, 0 },
    /* 5 */ { 0x01da, 23, 6, 0 },
    /* 6 */ { 0x00e5, 25, 7, 0 },
    /* 7 */ { 0x006f, 28, 8, 0 },
    /* 8 */ { 0x0036, 30, 9, 0 },
    /* 9 */ { 0x001a, 33, 10, 0 },
    /* 10 */ { 0x000d, 35, 11, 0 },
    /* 11 */ { 0x0006, 9, 12, 0 },
    /* 12 */ { 0x0003, 10, 13, 0 },
    /* 13 */ { 0x0001, 12, 13, 0 },
    /* 14 */ { 0x5a7f, 15, 15, 1 },
    /* 15 */ { 0x3f25, 36, 16, 0 },
    /* 16 */ { 0x2cf2, 38, 17, 0 },
    /* 17 */ { 0x207c, 39, 18, 0 },
    /* 18 */ { 0x17b9, 40, 19, 0 },
    /* 19 */ { 0x1182, 42, 20, 0 },
    /* 20 */ { 0x0cef, 43, 21, 0 },
    /* 21 */ { 0x09a1, 45, 22, 0 },
    /* 22 */ { 0x072f, 46, 23, 0 },
    /* 23 */ { 0x055c, 48, 24, 0 },
    /* 24 */ { 0x0406, 49, 25, 0 },
    /* 25 */ { 0x0303, 51, 26, 0 },
    /* 26 */ { 0x0240, 52, 27, 0 },
    /* 27 */ { 0x01b1, 54, 28, 0 },
    /* 28 */ { 0x0144, 56, 29, 0 },
    /* 29 */ { 0x00f5, 57, 30, 0 },
    /* 30 */ { 0x00b7, 59, 31, 0 },
    /* 31 */ { 0x008a, 60, 32, 0 },
    /* 32 */ { 0x0068, 62, 33, 0 },
    /* 33 */ { 0x004e, 63, 34, 0 },
    /* 34 */ { 0x003b, 32, 35, 0 },
    /* 35 */ { 0x002c, 33, 9, 0 },
    /* 36 */ { 0x5ae1, 37, 37, 1 },
    /* 37 */ { 0x484c, 64, 38, 0 },
    /* 38 */ { 0x3a0d, 65, 39, 0 },
    /* 39 */ { 0x2ef1, 67, 40, 0 },
    /* 40 */ { 0x261f, 68, 41, 0 },
    /* 41 */ { 0x1f33, 69, 42, 0 },
    /* 42 */ { 0x19a8, 70, 43, 0 },
    /* 43 */ { 0x1518, 72, 44, 0 },
    /* 44 */ { 0x1177, 73, 45, 0 },
    /* 45 */ { 0x0e74, 74, 46, 0 },
    /* 46 */ { 0x0bfb, 75, 47, 0 },
    /* 47 */ { 0x09f8, 77, 48, 0 },
    /* 48 */ { 0x0861, 78, 49, 0 },
    /* 49 */ { 0x0706, 79, 50, 0 },
    /* 50 */ { 0x05cd, 48, 51, 0 },
    /* 51 */ { 0x04de, 50, 52, 0 },
    /* 52 */ { 0x040f, 50, 53, 0 },
    /* 53 */ { 0x0363, 51, 54, 0 },
    /* 54 */ { 0x02d4, 52, 55, 0 },
    /* 55 */ { 0x025c, 53, 56, 0 },
    /* 56 */ { 0x01f8, 54, 57, 0 },
    /* 57 */ { 0x01a4, 55, 58, 0 },
    /* 58 */ { 0x0160, 56, 59, 0 },
    /* 59 */ { 0x0125, 57, 60, 0 },
    /* 60 */ { 0x00f6, 58, 61, 0 },
    /* 61 */ { 0x00cb, 59, 62, 0 },
    /* 62 */ { 0x00ab, 61, 63, 0 },
    /* 63 */ { 0x008f, 61, 32, 0 },
    /* 64 */ { 0x5b12, 65, 65, 1 },
    /* 65 */ { 0x4d04, 80, 66, 0 },
    /* 66 */ { 0x412c, 81, 67, 0 },
    /* 67 */ { 0x37d8, 82, 68, 0 },
    /* 68 */ { 0x2fe8, 83, 69, 0 },
    /* 69 */ { 0x293c, 84, 70, 0 },
    /* 70 */ { 0x2379, 86, 71, 0 },
    /* 71 */ { 0x1edf, 87, 72, 0 },
    /* 72 */ { 0x1aa9, 87, 73, 0 },
    /* 73 */ { 0x174e, 72, 74, 0 },
    /* 74 */ { 0x1424, 72, 75, 0 },
    /* 75 */ { 0x119c, 74, 76, 0 },
    /* 76 */ { 0x0f6b, 74, 77, 0 },
    /* 77 */ { 0x0d51, 75, 78, 0 },
    /* 78 */ { 0x0bb6, 77, 79, 0 },
    /* 79 */ { 0x0a40, 77, 48, 0 },
    /* 80 */ { 0x5832, 80, 81, 1 },
    /* 81 */ { 0x4d1c, 88, 82, 0 },
    /* 82 */ { 0x438e, 89, 83, 0 },
    /* 83 */ { 0x3bdd, 90, 84, 0 },
    /* 84 */ { 0x34ee, 91, 85, 0 },
    /* 85 */ { 0x2eae, 92, 86, 0 },
    /* 86 */ { 0x299a, 93, 87, 0 },
    /* 87 */ { 0x2516, 86, 71, 0 },
    /* 88 */ { 0x5570, 88, 89, 1 },
    /* 89 */ { 0x4ca9, 95, 90, 0 },
    /* 90 */ { 0x44d9, 96, 91, 0 },
    /* 91 */ { 0x3e22, 97, 92, 0 },
    /* 92 */ { 0x3824, 99, 93, 0 },
    /* 93 */ { 0x32b4, 99, 94, 0 },
    /* 94 */ { 0x2e17, 93, 86, 0 },
    /* 95 */ { 0x56a8, 95, 96, 1 },
    /* 96 */ { 0x4f46, 101, 97, 0 },
    /* 97 */ { 0x47e5, 102, 98, 0 },
    /* 98 */ { 0x41cf, 103, 99, 0 },
    /* 99 */ { 0x3c3d, 104, 100, 0 },
    /* 100 */ { 0x375e, 99, 93, 0 },
    /* 101 */ { 0x5231, 105, 102, 0 },
    /* 102 */ { 0x4c0f, 106, 103, 0 },
    /* 103 */ { 0x4639, 107, 104, 0 },
    /* 104 */ { 0x415e, 103, 99, 0 },
    /* 105 */ { 0x5627, 105, 106, 1 },
    /* 106 */ { 0x50e7, 108, 107, 0 },
    /* 107 */ { 0x4b85, 109, 103, 0 },
    /* 108 */ { 0x5597, 110, 109, 0 },
    /* 109 */ { 0x504f, 111, 107, 0 },
    /* 110 */ { 0x5a10, 110, 111, 1 },
    /* 111 */ { 0x5522, 112, 109, 0 },
    /* 112 */ { 0x59eb, 112, 111, 1 }
};


byte qcoder_input_byte( bitstream_t* stream )
{
    byte buffer = stream->stream_buffer[stream->frame_bitoffset/8];
    stream->frame_bitoffset += 8;
    return buffer;
}

void qcoder_byte_in( qcoder_env_t* qcoder )
{
    qcoder->code_bits = 8;
    if ( qcoder->isEof ) return;
    qcoder->b = qcoder_input_byte( currStream );
    if ( qcoder->b == 0xFF )
    {
        qcoder->isEof = ( qcoder_input_byte( currStream ) != 0x01 );
        if ( qcoder->isEof ) return;
    }
    qcoder->code += qcoder->b << 8;
}


void qcoder_init( qcoder_env_t* qcoder, bitstream_t* currStream )
{
    int i, j;
    // advance input stream to byte alignment
    qcoder->isEof = 0;
    qcoder->interval = 0x10000;
    qcoder->code = 0;
    qcoder_byte_in( qcoder );
    qcoder->code <<= 8;
    qcoder_byte_in( qcoder );
    qcoder->code <<= 8;
    qcoder_byte_in( qcoder );

    qcoder->eq_prob_context.mps = qcoder->eq_prob_context.state = 0;
    qcoder->eq_prob_context.prob_fsm = &( eq_prob_fsm[0] );

    qcoder->cx_eob_flag.mps = qcoder->cx_eob_flag.state = 0;
    qcoder->cx_eob_flag.prob_fsm = standard_prob_fsm;

    for( i=0; i<NUM_COEFF_B8_CX_QCODER_R_MAX; i++ )
    {
        for( j=0; j<NUM_COEFF_B8_CX_QCODER_C_MAX; j++ )
        {
            qcoder->cx_luma_8x8[i][j].mps = qcoder->cx_luma_8x8[i][j].state = 0;
            qcoder->cx_luma_8x8[i][j].prob_fsm = standard_prob_fsm;

            qcoder->cx_chroma_8x8[i][j].mps = qcoder->cx_chroma_8x8[i][j].state = 0;
            qcoder->cx_chroma_8x8[i][j].prob_fsm = standard_prob_fsm;
        }
    }

    for( i=0; i<NUM_COEFF_B4_CX_QCODER_R_MAX; i++ )
    {
        for( j=0; j<NUM_COEFF_B4_CX_QCODER_C_MAX; j++ )
        {
            qcoder->cx_luma_4x4[i][j].mps = qcoder->cx_luma_4x4[i][j].state = 0;
            qcoder->cx_luma_4x4[i][j].prob_fsm = standard_prob_fsm;
        }
    }

    for( i=0; i<NUM_COEFF_B16_CX_QCODER_R_MAX; i++ )
    {
        for( j=0; j<NUM_COEFF_B16_CX_QCODER_C_MAX; j++ )
        {
            qcoder->cx_luma_16x16[i][j].mps = qcoder->cx_luma_16x16[i][j].state = 0;
            qcoder->cx_luma_16x16[i][j].prob_fsm = standard_prob_fsm;
        }
    }

    for( i=0; i<NUM_CBP_CX_QCODER_INTRA; i++ )
    {
        qcoder->cx_intra_cbp[i].mps = qcoder->cx_intra_cbp[i].state = 0;
        qcoder->cx_intra_cbp[i].prob_fsm = standard_prob_fsm;
    }

    for( i=0; i<NUM_CBP_CX_QCODER_INTER; i++ )
    {
        qcoder->cx_inter_cbp[i].mps = qcoder->cx_inter_cbp[i].state = 0;
        qcoder->cx_inter_cbp[i].prob_fsm = standard_prob_fsm;
    }

    for( i=0; i<NUM_DELTAQP_CX_QCODER; i++ )
    {
        qcoder->cx_delta_qp[i].mps = qcoder->cx_delta_qp[i].state = 0;
        qcoder->cx_delta_qp[i].prob_fsm = standard_prob_fsm;
    }

    for( i=0; i<NUM_INTRAMODE_CX_QCODER; i++ )
    {
        qcoder->cx_intra_luma_mode[i].mps = qcoder->cx_intra_luma_mode[i].state = 0;
        qcoder->cx_intra_luma_mode[i].prob_fsm = standard_prob_fsm;

        qcoder->cx_intra_chroma_mode[i].mps = qcoder->cx_intra_chroma_mode[i].state = 0;
        qcoder->cx_intra_chroma_mode[i].prob_fsm = standard_prob_fsm;
    }


    qcoder->cx_inter_ref.mps = qcoder->cx_inter_ref.state = 0;
    qcoder->cx_inter_ref.prob_fsm = standard_prob_fsm;


    for( i=0; i<NUM_MVD_CX_QCODER_MAX; i++ )
    {
        qcoder->cx_mvd_x[i].mps = qcoder->cx_mvd_x[i].state = 0;
        qcoder->cx_mvd_x[i].prob_fsm = standard_prob_fsm;

        qcoder->cx_mvd_y[i].mps = qcoder->cx_mvd_y[i].state = 0;
        qcoder->cx_mvd_y[i].prob_fsm = standard_prob_fsm;
    }

    qcoder->cx_transform_s16_flag.mps = qcoder->cx_transform_s16_flag.state = 0;
    qcoder->cx_transform_s16_flag.prob_fsm = standard_prob_fsm;

    qcoder->cx_transform_s4_flag.mps = qcoder->cx_transform_s4_flag.state = 0;
    qcoder->cx_transform_s4_flag.prob_fsm = standard_prob_fsm;

    for( i=0; i<NUM_MBTYPE_CX_QCODER; i++ )
    {
        qcoder->cx_mbtype[i].mps = qcoder->cx_mbtype[i].state = 0;
        qcoder->cx_mbtype[i].prob_fsm = standard_prob_fsm;
    }

    for( i=0; i<NUM_INTERSKIP_CX_QCODER; i++ )
    {
        qcoder->cx_inter_skip[i].mps = qcoder->cx_inter_skip[i].state = 0;
        qcoder->cx_inter_skip[i].prob_fsm = standard_prob_fsm;
    }

    for( i=0; i<NUM_PARTITION; i++ )
    {
        int j;
        for( j=0; j< NUM_DIRECTION; j++ )
        {
            qcoder->cx_mbsubtype_Direction[i][j].mps = qcoder->cx_mbsubtype_Direction[i][j].state = 0;
            qcoder->cx_mbsubtype_Direction[i][j].prob_fsm = standard_prob_fsm;
        }
    }

    for( i=0; i<NUM_MBSUBTYPE_CX_QCODER; i++ )
    {
        qcoder->cx_mbsubtype[i].mps = qcoder->cx_mbsubtype[i].state = 0;
        qcoder->cx_mbsubtype[i].prob_fsm = standard_prob_fsm;
    } //xy_110506
    //added by xy_110703 begin
    for( i=0; i<NUM_BLOCKSIZE_CX_QCODER; i++ )
    {
        qcoder->cx_blocksize[i].mps = qcoder->cx_blocksize[i].state = 0;
        qcoder->cx_blocksize[i].prob_fsm = standard_prob_fsm;
    }

    for( i=0; i<NUM_SUBBLOCKSIZE_CX_QCODER; i++ )
    {
        qcoder->cx_subblocksize[i].mps = qcoder->cx_subblocksize[i].state = 0;
        qcoder->cx_subblocksize[i].prob_fsm = standard_prob_fsm;
    }

    for( i=0; i<NUM_PRED_DIRECTION_CX_QCODER; i++ )
    {
        qcoder->cx_pred_direction[i].mps = qcoder->cx_pred_direction[i].state = 0;
        qcoder->cx_pred_direction[i].prob_fsm = standard_prob_fsm;
    }

    for( i=0; i<64; i++ )
    {
        qcoder->cx_eob_flag_8x8[i].mps = 0;
        qcoder->cx_eob_flag_8x8[i].state = 0;
        qcoder->cx_eob_flag_8x8[i].prob_fsm = standard_prob_fsm;

        qcoder->cx_eob_flag_chroma[i].mps = 0;
        qcoder->cx_eob_flag_chroma[i].state = 0;
        qcoder->cx_eob_flag_chroma[i].prob_fsm = standard_prob_fsm;
    }

    for( i=0; i<16; i++ )
    {
        qcoder->cx_eob_flag_4x4[i].mps = 0;
        qcoder->cx_eob_flag_4x4[i].state = 0;
        qcoder->cx_eob_flag_4x4[i].prob_fsm = standard_prob_fsm;
    }
    return;
}

void qcoder_decoder_renorm( qcoder_env_t* qcoder )
{
    do
    {
        qcoder->interval <<= 1;
        qcoder->code <<= 1;
        if ( --qcoder->code_bits == 0 ) qcoder_byte_in( qcoder );
    }
    while ( qcoder->interval < 0x8000 );
}

int qcoder_decode_symbol( qcoder_env_t* qcoder, prob_context_t* contextp )
{
    qcoder->bin = contextp->mps;
    qcoder->state = contextp->state;
    qcoder->interval -= contextp->prob_fsm[qcoder->state].lps_interval;
    if ( ( qcoder->code >> 16 ) < qcoder->interval )
    {
        if ( qcoder->interval < 0x8000 )
        {
            if ( qcoder->interval < ( unsigned long )contextp->prob_fsm[qcoder->state].lps_interval )
            {
                qcoder->bin = ! qcoder->bin;
                if ( contextp->prob_fsm[qcoder->state].do_switch_mps )
                {
                    contextp->mps = qcoder->bin;
                }
                contextp->state = contextp->prob_fsm[qcoder->state].next_state_lps;
            }
            else
            {
                contextp->state = contextp->prob_fsm[qcoder->state].next_state_mps;
            }
            qcoder_decoder_renorm( qcoder );
        }
    }
    else
    {
        qcoder->code -= qcoder->interval << 16;
        if ( qcoder->interval < ( unsigned long )contextp->prob_fsm[qcoder->state].lps_interval )
        {
            contextp->state = contextp->prob_fsm[qcoder->state].next_state_mps;
        }
        else
        {
            qcoder->bin = ! qcoder->bin;
            if ( contextp->prob_fsm[qcoder->state].do_switch_mps )
            {
                contextp->mps = qcoder->bin;
            }
            contextp->state = contextp->prob_fsm[qcoder->state].next_state_lps;
        }
        qcoder->interval = contextp->prob_fsm[qcoder->state].lps_interval;
        qcoder_decoder_renorm( qcoder );
    }
    return qcoder->bin;
}

void qcoder_flush( qcoder_env_t* qcoder )
{
    //while (! qcoder->isEof) {
    //  qcoder->b = qcoder_input_byte(currStream);
    //  if (qcoder->b == 0xFF) {
    //    qcoder->isEof = (qcoder_input_byte(currStream) != 0x01);
    //  }
    //}
}

prob_context_t* select_context( qcoder_env_t* qcoder, int n, prob_context_t* contexts, int num_contexts )
{
    if ( n<num_contexts )
        return &( contexts[n] );
    else
        return &( qcoder->eq_prob_context );
}

int aricod_decode_flag( qcoder_env_t* qcoder, prob_context_t* flag_context )
{
    return qcoder_decode_symbol( qcoder, &( flag_context[0] ) );
}



int aricod_decode_signed_fixed_bits( qcoder_env_t* qcoder, prob_context_t* contexts, int num_contexts, int nBits )
{
    int n = 0, i;
    int value = 0;
    int isNegative;

    prob_context_t* contextp = select_context( qcoder, n++, contexts, num_contexts );
    if ( qcoder_decode_symbol( qcoder, contextp ) ) return 0;
    contextp = select_context( qcoder, n++, contexts, num_contexts );
    isNegative = qcoder_decode_symbol( qcoder, contextp );
    i = nBits - 1;
    do
    {
        contextp = select_context( qcoder, n++, contexts, num_contexts );
        if ( qcoder_decode_symbol( qcoder, contextp ) ) value |= 1 << i;
    }
    while ( --i >= 0 );
    value += 1;
    if ( isNegative ) value = -value;
    return value;
}

int aricod_decode_unary( qcoder_env_t* qcoder, prob_context_t* contexts, int num_contexts )
{
    int n = 0;
    int value = 0;
    while ( qcoder_decode_symbol( qcoder, &( contexts[n] ) ) )
    {
        value += 1;
        // advance to next context if sufficient contexts
        if ( n < num_contexts - 1 ) n++;
    }
    return value;
}

int aricod_decode_signed_unary( qcoder_env_t* qcoder, prob_context_t* contexts, int num_contexts )
{
    int n = 0;
    int value = 0;
    int isPositive;
    while ( qcoder_decode_symbol( qcoder, &( contexts[n] ) ) )
    {
        value += 1;
        // advance to next context if sufficient contexts
        if ( n < num_contexts - 1 ) n++;
    }
    // perform inverse of sign folding
    isPositive = value & 1;
    value += 1;
    value >>= 1;
    if ( ! isPositive ) value = -value;
    return value;
}

int aricod_decode_trunary( qcoder_env_t* qcoder, prob_context_t* contexts, int num_contexts, int maxValue )
{
    int n = 0;
    int value = 0;
    while ( value < maxValue && qcoder_decode_symbol( qcoder, &( contexts[n] ) ) )
    {
        value += 1;
        // advance to next context if sufficient contexts
        if ( n < num_contexts - 1 ) n++;
    }
    return value;
}


int aricod_decode_expGolomb( qcoder_env_t* qcoder, prob_context_t* contexts, int num_contexts, int k )
{
    int n = 0;
    int value = 0;
    if ( qcoder_decode_symbol( qcoder, &( contexts[n] ) ) ) return value;
    if ( n < num_contexts - 1 ) n++;
    while ( qcoder_decode_symbol( qcoder, &( contexts[n] ) ) )
    {
        if ( n < num_contexts - 1 ) n++;
        value |= 1 << k++;
    }
    while ( k-- > 0 )
    {
        if ( qcoder_decode_symbol( qcoder, &( qcoder->eq_prob_context ) ) )
            value += ( 1 << k );
    }
    value += 1;
    return value;
}

int aricod_decode_signed_expGolomb( qcoder_env_t* qcoder, prob_context_t* contexts, int num_contexts, int k )
{
    int n = 0;
    int value = 0;
    int isNegative;

    if ( qcoder_decode_symbol( qcoder, &( contexts[n] ) ) ) return value;
    if ( n < num_contexts - 1 ) n++;
    isNegative = qcoder_decode_symbol( qcoder, &( contexts[n] ) );
    if ( n < num_contexts - 1 ) n++;
    while ( qcoder_decode_symbol( qcoder, &( contexts[n] ) ) )
    {
        if ( n < num_contexts - 1 ) n++;
        value |= 1 << k++;
    }
    while ( k-- > 0 )
    {
        if ( qcoder_decode_symbol( qcoder, &( qcoder->eq_prob_context ) ) )
            value += ( 1 << k );
    }
    value += 1;
    if ( isNegative ) value = -value;
    return value;
}

int qcoder_decode_eobflag( qcoder_env_t* qcoder )
{
    return aricod_decode_flag( qcoder, &( qcoder->cx_eob_flag ) );
}

int qcoder_decode_eobflag_8x8( qcoder_env_t* qcoder, int ipos )
{
    return aricod_decode_flag( qcoder, &( qcoder->cx_eob_flag_8x8[ipos] ) );
}

int qcoder_decode_eobflag_4x4( qcoder_env_t* qcoder, int ipos )
{
    return aricod_decode_flag( qcoder, &( qcoder->cx_eob_flag_4x4[ipos] ) );
}

int qcoder_decode_eobflag_chroma( qcoder_env_t* qcoder, int ipos )
{
    return aricod_decode_flag( qcoder, &( qcoder->cx_eob_flag_chroma[ipos] ) );
}

int qcoder_decode_delta_qp( qcoder_env_t* qcoder )
{
    return aricod_decode_signed_unary( qcoder, &( qcoder->cx_delta_qp[0] ), NUM_DELTAQP_CX_QCODER );
}

int qcoder_decode_luma_b8x8_coeff( qcoder_env_t* qcoder, int idx, int intra )
{
    if( intra )
        return aricod_decode_signed_expGolomb( qcoder, &( qcoder->cx_luma_8x8[idx<( NUM_COEFF_B8_CX_QCODER_R_INTRA-1 ) ? idx : ( NUM_COEFF_B8_CX_QCODER_R_INTRA-1 )][0] ), NUM_COEFF_B8_CX_QCODER_C_INTRA, 0 );
    else
        return aricod_decode_signed_expGolomb( qcoder, &( qcoder->cx_luma_8x8[idx<( NUM_COEFF_B8_CX_QCODER_R_INTER-1 ) ? idx : ( NUM_COEFF_B8_CX_QCODER_R_INTER-1 )][0] ), NUM_COEFF_B8_CX_QCODER_C_INTER, 0 );
}

int qcoder_decode_chroma_b8x8_coeff( qcoder_env_t* qcoder, int idx, int intra )
{
    if( intra )
        return aricod_decode_signed_expGolomb( qcoder, &( qcoder->cx_chroma_8x8[idx<( NUM_COEFF_B8_CX_QCODER_R_INTRA_CHROMA-1 ) ? idx : ( NUM_COEFF_B8_CX_QCODER_R_INTRA_CHROMA-1 )][0] ), NUM_COEFF_B8_CX_QCODER_C_INTRA_CHROMA, 0 );
    else
        return aricod_decode_signed_expGolomb( qcoder, &( qcoder->cx_chroma_8x8[idx<( NUM_COEFF_B8_CX_QCODER_R_INTER_CHROMA-1 ) ? idx : ( NUM_COEFF_B8_CX_QCODER_R_INTER_CHROMA-1 )][0] ), NUM_COEFF_B8_CX_QCODER_C_INTER_CHROMA, 0 );
}

int qcoder_decode_luma_b4x4_coeff( qcoder_env_t* qcoder, int idx, int intra )
{
    if( intra )
        return aricod_decode_signed_expGolomb( qcoder, &( qcoder->cx_luma_4x4[idx<( NUM_COEFF_B4_CX_QCODER_R_INTRA-1 ) ? idx : ( NUM_COEFF_B4_CX_QCODER_R_INTRA-1 )][0] ), NUM_COEFF_B4_CX_QCODER_C_INTRA, 0 );
    else
        return aricod_decode_signed_expGolomb( qcoder, &( qcoder->cx_luma_4x4[idx<( NUM_COEFF_B4_CX_QCODER_R_INTER-1 ) ? idx : ( NUM_COEFF_B4_CX_QCODER_R_INTER-1 )][0] ), NUM_COEFF_B4_CX_QCODER_C_INTER, 0 );
}

int qcoder_decode_luma_b16x16_coeff( qcoder_env_t* qcoder, int idx, int intra )
{
    if( intra )
        return aricod_decode_signed_expGolomb( qcoder, &( qcoder->cx_luma_16x16[idx<( NUM_COEFF_B16_CX_QCODER_R_INTRA-1 ) ? idx : ( NUM_COEFF_B16_CX_QCODER_R_INTRA-1 )][0] ), NUM_COEFF_B16_CX_QCODER_C_INTRA, 0 );
    else
        return aricod_decode_signed_expGolomb( qcoder, &( qcoder->cx_luma_16x16[idx<( NUM_COEFF_B16_CX_QCODER_R_INTER-1 ) ? idx : ( NUM_COEFF_B16_CX_QCODER_R_INTER-1 )][0] ), NUM_COEFF_B16_CX_QCODER_C_INTER, 0 );
}

int qcoder_decode_intra_luma_mode( qcoder_env_t* qcoder )
{
    //return aricod_decode_expGolomb(qcoder, &(qcoder->cx_intra_luma_mode[0]), 3, 0);
    return aricod_decode_trunary( qcoder, &( qcoder->cx_intra_luma_mode[0] ), NUM_INTRAMODE_CX_QCODER, NUM_INTRAMODE_CX_QCODER ); //Trunary [9/17/2011 Will]
}

int qcoder_decode_intra_chroma_mode( qcoder_env_t* qcoder )
{
    //return aricod_decode_expGolomb(qcoder, &(qcoder->cx_intra_chroma_mode[0]), 3, 0);
    return aricod_decode_trunary( qcoder, &( qcoder->cx_intra_chroma_mode[0] ), NUM_INTRAMODE_CX_QCODER, NUM_INTRAMODE_CX_QCODER ); //Trunary [9/17/2011 Will]
}

int qcoder_decode_inter_ref( qcoder_env_t* qcoder )
{
    return aricod_decode_flag( qcoder, &( qcoder->cx_inter_ref ) );
}

int aricod_decode_fixed_bits( qcoder_env_t* qcoder, prob_context_t* contexts, int num_contexts, int nBits )
{
    int n = 0, i = nBits;
    int value = 0;
    prob_context_t* contextp;
    do
    {
        contextp = select_context( qcoder, n++, contexts, num_contexts );
        if ( qcoder_decode_symbol( qcoder, contextp ) )
            value |= 1 << ( i-1 );
    }
    while ( --i > 0 );
    return value;
}

int qcoder_decode_inter_skip( qcoder_env_t* qcoder )
{
    return aricod_decode_fixed_bits( qcoder, &( qcoder->cx_inter_skip[0] ), NUM_INTERSKIP_CX_QCODER, 1 );
}


int qcoder_decode_blocksize( qcoder_env_t* qcoder )
{
    //return aricod_decode_fixed_bits(qcoder, &(qcoder->cx_blocksize[0]), NUM_BLOCKSIZE_CX_QCODER, 1);
    return aricod_decode_flag( qcoder, &( qcoder->cx_blocksize[0] ) );
}

int qcoder_decode_subblocksize( qcoder_env_t* qcoder )
{
    //return aricod_decode_fixed_bits(qcoder, &(qcoder->cx_subblocksize[0]), NUM_SUBBLOCKSIZE_CX_QCODER, 1);
    return aricod_decode_flag( qcoder, &( qcoder->cx_subblocksize[0] ) );
}

int qcoder_decode_pred_direction( qcoder_env_t* qcoder )
{
    return ( aricod_decode_fixed_bits( qcoder, &( qcoder->cx_pred_direction[0] ), NUM_PRED_DIRECTION_CX_QCODER, 2 ) + 3 )%4;
}

int qcoder_decode_mvdx( qcoder_env_t* qcoder )
{
    return aricod_decode_signed_expGolomb( qcoder, &( qcoder->cx_mvd_x[0] ), NUM_MVD_CX_QCODER_MAX, 0 );
}

int qcoder_decode_mvdy( qcoder_env_t* qcoder )
{
    return aricod_decode_signed_expGolomb( qcoder, &( qcoder->cx_mvd_y[0] ), NUM_MVD_CX_QCODER_MAX, 0 );
}

int qcoder_decode_mvdx_num( qcoder_env_t* qcoder, int cn )
{
    return aricod_decode_signed_expGolomb( qcoder, &( qcoder->cx_mvd_x[0] ), cn, 0 );
}

int qcoder_decode_mvdy_num( qcoder_env_t* qcoder, int cn )
{
    return aricod_decode_signed_expGolomb( qcoder, &( qcoder->cx_mvd_y[0] ), cn, 0 );
}


int qcoder_decode_transform_s4_flag( qcoder_env_t* qcoder )
{
    return aricod_decode_flag( qcoder, &( qcoder->cx_transform_s4_flag ) );
}

int qcoder_decode_transform_s16_flag( qcoder_env_t* qcoder )
{
    return aricod_decode_flag( qcoder, &( qcoder->cx_transform_s16_flag ) );
}
